// https://leetcode.cn/problems/surface-area-of-3d-shapes/
// Created by ade on 2022/8/19.
//
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

class Solution {
public:
    // 规则的可以这么算
    int surfaceArea(vector <vector<int>> &grid) {
        int cubes = 0, faces = 0;
        int n = grid.size();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cubes += grid[i][j];
                if (grid[i][j]) faces += grid[i][j] - 1;
                if (i) faces += min(grid[i][j], grid[i - 1][j]);
                if (j) faces += min(grid[i][j], grid[i][j - 1]);
            }
        }

        return cubes * 6 - faces * 2;
    }
};

int main() {
    Solution so;
    vector <vector<int>> grid = {{1, 2},
                                 {3, 4}}; // 1 + 2 + 3 + 1 + 1 + 2 + 3 = 13
//    vector <vector<int>> grid = {{2, 2, 2},
//                                 {2, 1, 2},
//                                 {2, 2, 2}};
    cout << so.surfaceArea(grid) << endl;
    return 0;
}